﻿using System;
using System.Collections.Generic;
using System.Linq;
using TuxedoLib;

namespace SpaceSquirrel.Automation
{
	public class MoonSquidAutomation : SpriteAutomation
	{
		private const double CHASE_DISTANCE_TILES = 6;
		private int counter = 0;
		private bool goLeft;

		public MoonSquidAutomation(Sprite sprite)
			: base(sprite)
		{
			this.goLeft = Util.RandomBool();
		}

		public override void DoStuff(PlayScene scene)
		{
			this.counter++;
			Sprite player = scene.Player;
			sprite = this.sprite;
			double dx = player.x - sprite.x;
			double dy = player.y - sprite.y;
			if (dx * dx + dy * dy < Math.Pow(16 * CHASE_DISTANCE_TILES, 2))
			{
				double v = (Math.Sin(this.counter / 30.0 * 2 * 3.14159 / 2) + 1) / 1.5;
				if (dx == 0 && dy == 0)
				{
				}
				else
				{
					double d = Math.Sqrt(dx * dx + dy * dy);
					dx *= v / d;
					dy *= v / d;
					sprite.dx = dx;
					sprite.dy = dy;
				}
			}
			else
			{
				if (sprite.collideWall)
				{
					this.goLeft = !this.goLeft;
				}
				else
				{
					sprite.dx = this.goLeft ? -1 : 1;
				}
			}
		}
	}
}
